package cn.songhaiqing.walle.bean;

import android.util.Log;

import cn.songhaiqing.walle.ble_controller.NoConnectException;
import cn.songhaiqing.walle.help.SpHelper;
import cn.songhaiqing.walle.synccontroller.SyncCompat;

public class Notification {
    public static final int SEND_NOTI_TYPE_FACEBOOK = 4;
    public static final int SEND_NOTI_TYPE_GMAIL = 6;
    public static final int SEND_NOTI_TYPE_INSTAGRAM = 12;
    public static final int SEND_NOTI_TYPE_KAKAOTALK = 14;
    public static final int SEND_NOTI_TYPE_LINE = 8;
    public static final int SEND_NOTI_TYPE_LINKEDIN = 11;
    public static final int SEND_NOTI_TYPE_MESSENGER = 10;
    public static final int SEND_NOTI_TYPE_PHONE = 2;
    public static final int SEND_NOTI_TYPE_QQ = 0;
    public static final int SEND_NOTI_TYPE_SKYPE = 9;
    public static final int SEND_NOTI_TYPE_SMS = 3;
    public static final int SEND_NOTI_TYPE_TWITTER = 5;
    public static final int SEND_NOTI_TYPE_VIBER = 13;
    public static final int SEND_NOTI_TYPE_VKONTAKTE = 15;
    public static final int SEND_NOTI_TYPE_WECHAT = 1;
    public static final int SEND_NOTI_TYPE_WHATSUP = 7;
    private final String TAG = "Notification";
    private SyncCompat compat;
    private String content;
    private int type;

    public Notification(SyncCompat compat2, int type2, String content2) {
        this.compat = compat2;
        this.type = type2;
        this.content = content2;
    }

    public void sendNotifi() throws IllegalAccessException {
        String send;
        int count = getPackageCount();
        int id = SpHelper.getInt(SpHelper.NOTIFICATION_SEND_ID, 1);
        for (int i = 0; i < count; i++) {
            if (i != count - 1) {
                send = this.content.substring(i * 8, (i * 8) + 8);
            } else if (count * 8 > this.content.length()) {
                send = this.content.substring(i * 8, this.content.length());
            } else {
                send = this.content.substring(i * 8, count * 8);
            }
            Log.d("Notification", "send:" + send + "    id:" + id + "   type" + this.type + "    count:" + count);
            try {
                this.compat.writeData(getBleSendValue(count, this.type, id, send.toCharArray()));
            } catch (NoConnectException e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
        }
        if (count == 0) {
            byte[] value = new byte[20];
            value[0] = 32;
            value[1] = (byte) id;
            value[2] = 1;
            value[3] = (byte) this.type;
            try {
                this.compat.writeData(value);
            } catch (NoConnectException e3) {
                e3.printStackTrace();
            }
        }
        if (id == 255) {
            SpHelper.putInt(SpHelper.NOTIFICATION_SEND_ID, 1);
        } else {
            SpHelper.putInt(SpHelper.NOTIFICATION_SEND_ID, id + 1);
        }
    }

    public int getPackageCount() {
        int count;
        if (this.content.length() > 48) {
            count = 6;
            this.content = this.content.substring(0, 47);
            this.content += "…";
        } else if (this.content.length() == 48) {
            count = 6;
        } else if (this.content == null || this.content.length() == 0) {
            return 0;
        } else {
            int merchant = this.content.length() / 8;
            if (this.content.length() % 8 == 0) {
                count = merchant;
            } else {
                count = merchant + 1;
            }
        }
        return count;
    }

    private byte[] getBleSendValue(int count, int type2, int id, char[] content2) throws IllegalAccessException {
        if (content2.length > 8) {
            throw new IllegalAccessException("content长度大于8");
        }
        byte[] value = new byte[20];
        value[0] = 32;
        value[1] = (byte) id;
        value[2] = (byte) count;
        value[3] = (byte) type2;
        for (int i = 0; i < content2.length; i++) {
            byte[] b = charToByte(content2[i]);
            value[(i * 2) + 4] = b[0];
            value[(i * 2) + 5] = b[1];
        }
        if (content2.length < 8) {
            for (int i2 = (content2.length * 2) + 4; i2 < value.length; i2++) {
                value[i2] = 0;
            }
        }
        return value;
    }

    public byte[] charToByte(char c) {
        return new byte[]{(byte) ((65280 & c) >> 8), (byte) (c & 255)};
    }
}
